﻿package game 
{
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.display.SimpleButton;
	import flash.events.DataEvent;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.text.TextField;
	import game.events.EventData;
	import game.exchangeddata.HashTable;
	
	import game.BetPlace;
	
	import common.Utility;
	import game.constants.ConstData;
	/**
	 * ...
	 * @author Nguyen Thi Bich Phuong (Daisy)
	 */
	public class BetController extends MovieClip {		
		public var confirmBtn:SimpleButton;
		public var clearBtn:SimpleButton;
		public var rebetBtn:SimpleButton;
		
		public var txtConfirmTf:TextField;
		public var txtClearTf:TextField;
		public var txtRebetTf:TextField;
		
		public var hitArr:Array;
		public var betArr:Array;
		
		public var chipController:ChipController;
		
		public var currentBalance:Number;
		
		public var minBet:Number = 0;
		public var maxBet:Number = 1000;
		
		public var isLock:Boolean = true;
		
		public var initChipValueArr:Array;
		
		public var betSuccess_mc:MovieClip;	
		public var tooltip_mc:MovieClip;
		
		private var _gameType:String;
		
		//public var chipInitPosition:Number = 0;
		//public var chip_controller_pos:int = 0;
		
		public function BetController() {
			this.betArr = new Array();		
			this.hitArr = new Array();
		
			this.showBetSuccess(false , true);
			this.tooltipOver(false, 0, 0, 0, 0);	
			
			/*if (this.chipInitPosition == 0) {
				this.chipInitPosition = this.chipController.x;
			}*/	
		}
		
		/**
		 * @author Daisy
		 * @param	minBet
		 * @param	maxBet
		 * @param	currentBalance
		 * @param	initChipValueArr
		 */
		
		public function set gameType(value:String):void {
			
			this._gameType = value;
		}
	
		public function get gameType():String {			
			return this._gameType;
		}
	
		public function initBet(minBet:Number, maxBet:Number, currentBalance:Number, initChipValueArr:Array):void {
			this.minBet = minBet;
			this.maxBet = maxBet;
			this.currentBalance = currentBalance;			
			this.initChipValueArr = initChipValueArr;			
			//draw chip
			if(this._gameType == ConstData.GAME_TYPE_SICBO)
				this.chipController.initChips(this.initChipValueArr);
			else if (this._gameType == ConstData.GAME_TYPE_BAC3) {
 				this.chipController.initChipsValue(56);
				this.chipController.bac3InitChips(this.maxBet);
			}				
			else{0
				this.chipController.initChipsValue(50);
				this.chipController.resetChipPos(this.chipController.getChipPostion(this.maxBet), 50);
			}	
			this.txtConfirmTf.mouseEnabled = false;
			this.txtClearTf.mouseEnabled = false;
			this.txtRebetTf.mouseEnabled = false;
			
			this.confirmBtn.useHandCursor = true;
									
			this.confirmBtn.addEventListener(MouseEvent.CLICK, confirmBtnClickHandler);
			this.clearBtn.addEventListener(MouseEvent.CLICK, clearBtnClickHandler);
			this.rebetBtn.addEventListener(MouseEvent.CLICK, rebetBtnClickHandler);
			this.tooltip_mc.addEventListener(MouseEvent.ROLL_OVER, tooltipOverHandler);			
			this.tooltip_mc.addEventListener(MouseEvent.ROLL_OUT, tooltipOutHandler);
		}
		
		
		
		private function tooltipOutHandler(e:MouseEvent):void {
			this.tooltip_mc.visible = false;
		}
		
		private function tooltipOverHandler(e:MouseEvent):void {			
			this.tooltip_mc.visible = true;
		}
		
		/**
		 * @author Daisy
		 * @param	e
		 */
		public function confirmBtnClickHandler(e:MouseEvent):void {
			//dispatchEvent(new Event(EventData.CONFIRM_BETTING, true));
		}
		
		/**
		 * @author Daisy
		 * @param	e
		 */
		public function clearBtnClickHandler(e:MouseEvent):void {
			
		}
		
		/**
		 * @author Daisy
		 * @param	e
		 */
		public function rebetBtnClickHandler(e:MouseEvent):void {
			
		}
		
		/**
		 * @author Daisy
		 * @param	hitArr
		 * @return
		 */
		protected function initBetPlace(hitArr:Array):Array {
			this.hitArr = hitArr;
			var betArr:Array = new Array();
			for (var i:int = 0; i < this.hitArr.length; i++) {
				var betPlace:BetPlace = new BetPlace(this.hitArr[i], this.initChipValueArr);				
				this.addChild(betPlace);
				
				var bet:BetPlace = BetPlace(betPlace);
				betArr.push(betPlace)
				bet.betHit.addEventListener(MouseEvent.MOUSE_DOWN, betPlaceMouseDownHandler);
				bet.betHit.addEventListener(MouseEvent.ROLL_OVER, betPlaceMouseOverHandler);
				bet.betHit.addEventListener(MouseEvent.ROLL_OUT, betPlaceMouseOutHandler);
			}
			return betArr;
		}
		
		private function betPlaceMouseOutHandler(e:MouseEvent):void {			
			tooltipOver(false, 0, 0, 0, 0);
			dispatchEvent(new EventData(EventData.BETPLACE_MOUSEOUT,true));
		}
		
		/**
		 * @author Daisy
		 * @param	e
		 */
		public function betPlaceMouseOverHandler(e:MouseEvent):void {				
			var betHit:DisplayObject = DisplayObject(e.currentTarget);
			var bet:BetPlace = BetPlace(betHit.parent);			
			var _val = bet.getBetValue();						
			tooltipOver(true, _val, 0, bet.x, bet.y);					
			
			if (this.isLock) {
				return;
			}
			var activeChip:Chip = chipController.activeChip;			
			GlobalVariables.chipActiveAmount = activeChip.value;			
			GlobalVariables.betPlaceActive = e.currentTarget.name;
			
			dispatchEvent(new EventData(EventData.BETPLACE_MOUSEOVER, true));			
		}
		
		/**
		 * @author Daisy
		 * @param	e
		 */
		public function betPlaceMouseDownHandler(e:MouseEvent):void {
			if (this.isLock) {
				return;
			}
			
			this.showBetSuccess(false, false);
			
			var activeChip:Chip = chipController.activeChip;
			var event:EventData;
			// Check minBet and maxBet
			if (Utility.fixNumber(this.getTotalBetValue() + activeChip.value) <= this.maxBet) {
				// Check Balance
				if (Utility.fixNumber(this.currentBalance - activeChip.value) >= 0) {					
					// Place Bet
					var betHit:DisplayObject = DisplayObject(e.currentTarget);
					var bet:BetPlace = BetPlace(betHit.parent);
					bet.addBet(activeChip);					
					this.currentBalance = Utility.fixNumber(this.currentBalance - activeChip.value)
					
					var _val = bet.getBetValue();			
					tooltipOver(true, _val, 0, bet.x, bet.y);
					event = new EventData(EventData.BET_CHANGE);
					event.newBalance = this.currentBalance;
					dispatchEvent(event);
				} else {
					event = new EventData(EventData.SHOW_MS, true);
					event.ms = "OverCredit";
					dispatchEvent(event);
				}
			}else {
				//Utility.showMessage("MAX BETTING");
				event = new EventData(EventData.SHOW_MS, true);
				event.ms = "OverUserUpperLimit";
				dispatchEvent(event);
			}
		}
		
		/**
		 * @author Daisy
		 * @return
		 */
		public function getTotalBetValue():Number {
			var totalBet:Number = 0;
			for (var i:int = 0; i < this.betArr.length; i++) {
				totalBet += BetPlace(this.betArr[i]).getBetValue();
			}
			//trace("getTotalBetValue:" + totalBet  + ":" + Utility.fixNumber(0));
			if (totalBet == 0)
				return 0;
			return Utility.fixNumber(totalBet);
		}
		
		/**
		 * @author Daisy 
		 */
		public function lock():void {
			this.isLock = true;
			for (var i in this.betArr) {
				BetPlace(this.betArr[i]).betHit.mouseEnabled = false;			
				SimpleButton(BetPlace(this.betArr[i]).betHit).useHandCursor = false;
			}
			confirmBtn.mouseEnabled = false;
			clearBtn.mouseEnabled = false;
			rebetBtn.mouseEnabled = false;
			
			for (var j = 0; j < this.chipController.chipArr.length; j++) {
				Chip(this.chipController.chipArr[j]).mouseEnabled = false;
			}
		}
		
		/**
		 * @author Daisy
		 */
		public function unlock():void {
			this.isLock = false;
			for (var i in this.betArr) {
				BetPlace(this.betArr[i]).betHit.mouseEnabled = true;
				SimpleButton(BetPlace(this.betArr[i]).betHit).useHandCursor = true;
			}
			confirmBtn.mouseEnabled = true;
			clearBtn.mouseEnabled = true;
			rebetBtn.mouseEnabled = true;
			for (var j = 0; j < this.chipController.chipArr.length; j++) {
				Chip(this.chipController.chipArr[j]).mouseEnabled = true;
		    }
		}
		
		/**
		 * @author Daisy
		 */
		public function clearBet():void {			
			this.currentBalance = Utility.fixNumber(this.currentBalance + getTotalBetValue());
			var event:EventData = new EventData(EventData.BET_CHANGE);
			event.newBalance = this.currentBalance;
			for (var i in betArr) {
				BetPlace(this.betArr[i]).clearBet();
			}
			dispatchEvent(event);					
		}
		
		public function clearStartBet():void {
			for (var i in betArr) {
				BetPlace(this.betArr[i]).clearBet();
			}
		}
		
		/**
		 * @author Daisy
		 * @param	info
		 * Need to be overrided in sub class
		 */		
		public function updateBet(info:Object):void {
			
		}
		
		/**
		 * @author Daisy
		 * @param	hashTableInfo
		 */
		public function initText(hashTableInfo:HashTable):void {
			//trace("initText");
			this.txtConfirmTf.text = hashTableInfo.lookupString("btnConfirm");
			this.txtClearTf.text = hashTableInfo.lookupString("btnClear");
			this.txtRebetTf.text = hashTableInfo.lookupString("btnRebet");
		}
		
		public function showBetSuccess(isShow:Boolean, isBet:Boolean):void {
			this.betSuccess_mc.visible = isShow;
			
			setChildIndex(this.betSuccess_mc, this.numChildren - 1);			
			
			//If Show betSuccess field the data
			if (isShow) {
				if (isBet) {					
					this.betSuccess_mc.betMessage.text =  GlobalVariables.langInfo.hashTable.lookupString("BSuccess");				
				} else {
					this.betSuccess_mc.betMessage.text = GlobalVariables.langInfo.hashTable.lookupString("NoBetPlace");					
				}
			}
		}
		
		public function tooltipOver(isShow:Boolean, val:Number, max:Number, _x:int, _y:int):void {
			if (isShow) {			
				//this.tooltip_mc.tooltip_txt.text = "MaxBet: " + Utility.currency(max,2,"") + "\n";
				//this.tooltip_mc.tooltip_txt.appendText("Current Bet: " + Utility.currency(val, 2, ""));
				this.tooltip_mc.tooltip_txt.text = GlobalVariables.langInfo.hashTable.lookupString("INFOMaxbet") + ": " + Utility.currency(max, 2, "") + "\n";
				this.tooltip_mc.tooltip_txt.appendText(GlobalVariables.langInfo.hashTable.lookupString("INFOCurrentbet") + ": " + Utility.currency(val,2,""));
				this.tooltip_mc.x = _x + 35;
				this.tooltip_mc.y = _y - 18;
				//setChildIndex(this.tooltip_mc, this.numChildren - 1);	
				this.tooltip_mc.visible = true
			} else {
				this.tooltip_mc.visible = false;
			}			
		}
		
		public function checkUpperLower(upper:Number, lower:Number, val:Number):Boolean {
			var event:EventData;
			var valueItem:String; 
			if (val < lower)
			{
				valueItem = GlobalVariables.langInfo.hashTable.lookupString("OverUserLowerLimit");
				event = new EventData(EventData.SHOW_MS, true);
				event.ms = valueItem + " : " + lower;//lower;
				trace("OverUserLowerLimit");
				dispatchEvent(event);
			}
			else if (val > upper)
			{
				valueItem = GlobalVariables.langInfo.hashTable.lookupString("OverUserUpperLimit");
				event = new EventData(EventData.SHOW_MS, true);
				event.ms = valueItem + " : " + upper;//lower;
				trace("OverUserUpperLimit");
				dispatchEvent(event);
			}
			else {
				return true;
			}
			return false;
		}
		
	}

}